package mvc.model.dao.impl;

import mvc.model.dao.AddrDao;
import mvc.model.pojos.AddressBean;

import java.util.List;

public class AddrDaoImpl implements AddrDao {
    @Override
    public void addAddress(AddressBean address) {
        String sql = "INSERT INTO `address` (`people`, `phone`, `province`, `city`, `county`, `village`, `addr`, `uid`, `isdefault`, `flag`) VALUES (?,?,?,?,?,?,?,?,?,?);";
        int update = basedao.update(sql, address.getPeople(), address.getPhone(), address.getProvince(),
                address.getCity(), address.getCounty(), address.getVillage(), address.getAddr(),
                address.getUid(), address.getIsdefault(), address.getFlag());

    }

    @Override
    public void upAddress(AddressBean address, int number) {
        String sql = "UPDATE `address` SET `people` = ?, `phone` = ?, `province` = ?, `city` = ?, `county` = ?, `village` = ?, `addr` = ?, `uid` = ?, `isdefault` = ?, `flag` = ? WHERE (`anumber` = ?);";
        int update = basedao.update(sql, address.getPeople(), address.getPhone(), address.getProvince(),
                address.getCity(), address.getCounty(), address.getVillage(), address.getAddr(),
                address.getUid(), address.getIsdefault(), address.getFlag(), number);

    }

    @Override
    public AddressBean selectOne(int isdfault, int uid) {
        String sql = "select * from `address` where `isdefault`=1 and `uid`=?;";
        List<AddressBean> query = basedao.query(sql, AddressBean.class, uid);
        return query.isEmpty() ? null : query.get(0);
    }

    @Override
    public List<AddressBean> select(int uid, int flag) {
        String sql = "select * from `address` where uid=? and flag=?;";
        return basedao.query(sql, AddressBean.class, uid, flag);
    }

    @Override
    public AddressBean queryOne(int anumber) {
        String sql = "select * from `address` where anumber=?;";
        List<AddressBean> query = basedao.query(sql, AddressBean.class, anumber);
        return query.isEmpty() ? null : query.get(0);
    }

}
